<?php

namespace App\Models;

use App\Traits\Model as ModelTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
use Grimzy\LaravelMysqlSpatial\Types\Point;


class SecurityUser extends Model
{
    use SpatialTrait;
    use HasFactory;
    use ModelTrait;

    /**
     * @var string[] 人员状态
     */
    public static $status = [1=>'休息',2=>'巡逻',3=>'安保'];

    protected $table='security_user';

    protected $appends = ['latitude', 'longitude'];

    protected $spatialFields = [
        'position'
    ];

    // 经纬度数据转换
    protected function setPositionAttribute($value){
        $this->attributes['position'] = new Point($this->attributes['latitude'], $this->attributes['longitude']);
        unset($this->attributes['latitude']);
        unset($this->attributes['longitude']);
    }

    public function getLatitudeAttribute(){
        return $this->attributes['position']->getLat();
    }

    public function getLongitudeAttribute(){
        return $this->attributes['position']->getLng();
    }

    public function car(){
        return $this->hasOne('App\Models\SecurityCar', 'id', 'car_id');
    }
}
